package MyServlet;

import java.io.IOException;
import java.sql.SQLException;
import factory.HospitalFactory;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.HospitalDao;
import MyBean.Message;
import MyBean.Program;
import MyBean.patient;
import factory.HospitalFactory;

/**
 * Servlet implementation class insertPatient
 */

public class insertProgram extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public insertProgram() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
			try {
				HospitalDao hd = HospitalFactory.getProxy();
				String programName = request.getParameter("programName");
				Message message = hd.searchMessageByName(programName);
				if(message!=null && hd.insertProgram(new Program(request.getParameter("admissionNumber"),programName,message.getPrice())))
				{
					request.setAttribute("program2",new Program(request.getParameter("admissionNumber"),programName,message.getPrice()));
					RequestDispatcher dispatcher = request.getRequestDispatcher("insertProgramSuccess.jsp");
					dispatcher.forward(request, response);
				} else {
					response.sendRedirect("insertProgramFail.jsp");
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
